/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.flealancemarket.service.facades.impl;

import com.googlecode.flealancemarket.metamodel.PrivateMessage;
import com.googlecode.flealancemarket.metamodel.User;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.googlecode.flealancemarket.service.facades.MessagesFacade;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author vanja
 */
public class MessagesFacadeImpl extends HibernateDaoSupport implements MessagesFacade {

    @Transactional
    public PrivateMessage savePrivateMessage(PrivateMessage pm) {
        Session s = getSessionFactory().getCurrentSession();
        s.saveOrUpdate(pm);
        return pm;
    }

    @Transactional
    public int countUnreadMessages(User user) {
        Session session = getSessionFactory().getCurrentSession();
        Criteria criteria = session.createCriteria(PrivateMessage.class);
        criteria
                .add(Restrictions.eq("receiver", user))
                .add(Restrictions.eq("read", false));
        return criteria.list().size();
    }

    @Transactional
    public List<PrivateMessage> getMessages(User user, int pageNumber) {
        Session session = getSessionFactory().getCurrentSession();
        Criteria criteria = session.createCriteria(PrivateMessage.class);
        criteria
                .add(Restrictions.eq("receiver", user))
                .addOrder(Order.desc("sendTime"))
                .setMaxResults(10)
                .setFirstResult(10 * (pageNumber - 1));
        return criteria.list();
    }

    @Transactional
    public int countMessages(User user) {
        Session session = getSessionFactory().getCurrentSession();
        Criteria criteria = session.createCriteria(PrivateMessage.class);
        criteria
                .add(Restrictions.eq("receiver", user));
        return criteria.list().size();
    }

    @Transactional
    public PrivateMessage getMessage(Long id) {
        Session session = getSessionFactory().getCurrentSession();
        Criteria criteria = session
                                .createCriteria(PrivateMessage.class)
                                .add(Restrictions.eq("_id", id));
        return (PrivateMessage)criteria.uniqueResult();
    }

    @Transactional
    public void deleteMessage(PrivateMessage pm) {
        Session s = getSessionFactory().getCurrentSession();
        s.delete(pm);
    }


    @Transactional
    public void deleteMessageById(Long id) {
        Session s = getSessionFactory().getCurrentSession();
        PrivateMessage pm = (PrivateMessage)s
                                .createCriteria(PrivateMessage.class)
                                .add(Restrictions.eq("_id", id))
                                .uniqueResult();
        deleteMessage(pm);
    }

    @Transactional
    public int countSentMessages(User user) {
        Session session = getSessionFactory().getCurrentSession();
        Criteria criteria = session.createCriteria(PrivateMessage.class);
        criteria
                .add(Restrictions.eq("sender", user));
        return criteria.list().size();
    }

    @Transactional
    public List<PrivateMessage> getSentMessages(User user, int pageNumber) {
        Session session = getSessionFactory().getCurrentSession();
        Criteria criteria = session.createCriteria(PrivateMessage.class);
        criteria
                .add(Restrictions.eq("sender", user))
                .addOrder(Order.desc("sendTime"))
                .setMaxResults(10)
                .setFirstResult(10 * (pageNumber - 1));
        return criteria.list();
    }

}
